package com.bocai.kelalacar.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.bocai.kelalacar.R;
import com.bocai.kelalacar.ui.car.main.presenter.MainPresenter;
import com.bocai.kelalacar.ui.car.order.act.MyCarOrderActivity;
import com.bocai.kelalacar.ui.car.set.act.CarManageActivity;
import com.bocai.kelalacar.ui.car.set.act.CertificationInformationActivity;
import com.bocai.kelalacar.ui.car.set.act.CertificationInformationStateActivity;
import com.bocai.kelalacar.ui.car.set.act.DriverMangerActivity;
import com.bocai.kelalacar.ui.car.set.act.MyWalletActivity;
import com.bocai.kelalacar.ui.car.set.act.ServiceMarginActivity;
import com.bocai.kelalacar.ui.car.set.act.SetActivity;
import com.bocai.kelalacar.ui.driver.set.act.DriverCertificationInformationActivity;
import com.bocai.kelalacar.ui.driver.set.act.DriverCertificationInformationStateActivity;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.util.ToastUitl;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by yuanfei on 2017/6/19.
 */

public class TeamUserPopupWindow extends PopupWindow implements View.OnClickListener {

    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.head_img)
    CircleImageView headImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_my_order)
    LinearLayout llMyOrder;
    @BindView(R.id.ll_my_wallet)
    LinearLayout llMyWallet;
    @BindView(R.id.ll_my_certification)
    LinearLayout llMyCertification;
    @BindView(R.id.ll_car_manage)
    LinearLayout llCarManage;
    @BindView(R.id.ll_driver_manage)
    LinearLayout llDriverManage;
    @BindView(R.id.ll_my_invoice)
    LinearLayout llMyInvoice;
    @BindView(R.id.ll_set)
    LinearLayout llSet;
    ImageView imgHide;
    private View menuView;
    private Activity mContext;
    LayoutInflater inflater;
    Button btn;
    TextView tvPayMoney;
    MainPresenter presenter;


    public TeamUserPopupWindow(Activity mContext, MainPresenter presenter) {
        this.mContext = mContext;
        this.presenter = presenter;

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        menuView = inflater.inflate(R.layout.act_team_user, null);
        ButterKnife.bind(this, menuView);
        imgHide = (ImageView) menuView.findViewById(R.id.img_right);
        imgHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        imgHide.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                backgroundAlpha(1f);
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        llMyOrder.setOnClickListener(this);
        llMyInvoice.setOnClickListener(this);
        llSet.setOnClickListener(this);
        llMyCertification.setOnClickListener(this);
        llMyWallet.setOnClickListener(this);
        llCarManage.setOnClickListener(this);
        llDriverManage.setOnClickListener(this);

    }

    public void setValue(double value) {
        tvPayMoney.setText("￥" + value);
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_my_invoice:
                mContext.startActivity(new Intent(mContext, ServiceMarginActivity.class));
                break;
            case R.id.ll_set:
                mContext.startActivityForResult(new Intent(mContext, SetActivity.class),3);
                break;
            case R.id.ll_my_certification:
                    if ((int) SP.get(mContext, "MemberType", 0) == 3) {
                        mContext.startActivity(new Intent(mContext, DriverCertificationInformationStateActivity.class));
                    } else {
                        mContext.startActivity(new Intent(mContext, CertificationInformationStateActivity.class));
                    }
                break;
            case R.id.ll_my_order:
                if ((int)SP.get(mContext,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(mContext,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(mContext,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    mContext.startActivity(new Intent(mContext, MyCarOrderActivity.class));
                }
                break;
            case R.id.ll_my_wallet:
                if ((int)SP.get(mContext,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(mContext,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(mContext,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    mContext.startActivity(new Intent(mContext, MyWalletActivity.class));
                }
                break;
            case R.id.ll_car_manage:
                if ((int)SP.get(mContext,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(mContext,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(mContext,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    mContext.startActivity(new Intent(mContext, CarManageActivity.class));
                }
                break;
            case R.id.ll_driver_manage:
                if ((int)SP.get(mContext,"ApprovalStatus",0) == 0){
                    ToastUitl.showShort("认证未申请");
                } else if ((int)SP.get(mContext,"ApprovalStatus",0) == -1){
                    ToastUitl.showShort("审核未通过");
                }else if ((int)SP.get(mContext,"ApprovalStatus",0) == 1){
                    ToastUitl.showShort("审核中");
                }else {
                    mContext.startActivity(new Intent(mContext, DriverMangerActivity.class));
                }
                break;
        }
    }
    /**
     * 设置数据
     */
    public void setData(){
        tvName.setText(SP.get(mContext,"PhoneNumber","").toString());
        Glide.with(mContext).load(SP.get(mContext,"Avatar2","").toString()).error(R.mipmap.head_img).into(headImg);

        if ((int)SP.get(mContext,"MemberType",0) == 3){
            llDriverManage.setVisibility(View.GONE);
            llCarManage.setVisibility(View.GONE);
        }else {
            llDriverManage.setVisibility(View.VISIBLE);
            llCarManage.setVisibility(View.VISIBLE);
        }
    }

    public void getUser(){
        if ((int)SP.get(mContext,"ApprovalStatus",0) == 0){
            presenter.getUser();
        }
    }

}
